<?php
/**
 *  * author 韩天尊
 * Date: 2022/3/15
 * Email: <724700483@qq.com>
 */

namespace App\HttpController\Api\InterfaceApi\App;

use App\HttpController\Api\InterfaceApi\InterfaceApiBase;
use App\Toolys\InterfaceData;
use EasySwoole\EasySwoole\Core;
use EasySwoole\EasySwoole\Trigger;
use EasySwoole\Http\Message\Status;
use EasySwoole\HttpAnnotation\Exception\Annotation\ParamValidateError;
use Exception;

/**
 * APP基类
 */
abstract class InterfaceApiAppBase extends InterfaceApiBase
{
    /**
     * @throws Exception
     */
    public function onRequest(?string $action): ?bool
    {
        if (parent::onRequest($action)) {
            // 白名单判断
            if (in_array($action, $this->whiteList)) {
                return true;
            }
            // 获取登入信息
            $request = $this->request();
            if(isset($request->getHeaders()['token'])==false || empty($request->getHeaders()['token'])){
                $this->failure("empty token", [], Status::CODE_UNAUTHORIZED);
                $this->writeBack->write_back_type = false; return false;
            }
            $checkData = verifyToken($request->getHeaders()['token'][0], "APP");
            if ($checkData->execute == false) {
                $this->failure($checkData->message, [], Status::CODE_UNAUTHORIZED);
                $this->writeBack->write_back_type = false; return false;
            }
            $this->currentTokenActionId = $checkData->data['objectInfo']['id'];
            $this->currentTokenOtherInfo = $checkData->data['objectInfo']['otherInfo'];
            $this->currentTokenServerInfo = $checkData->data['serverInfo'];
            return true;
        }
        return false;
    }
}